package cc.orange.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by CM on 2022/10/26.
 */

public class WxLogUtil {
   public static  IWXAPI api;
   public static Context  mContext;

   public static void initWx(Context  context,String appId){
      mContext = context;

      // 通过 WXAPIFactory 工厂，获取 IWXAPI 的实例
      api = WXAPIFactory.createWXAPI(context, appId, true);
      // 将应用的 appId 注册到微信
      api.registerApp(appId);
//
//        IWXAPI wxapi = WXAPIFactory.createWXAPI(this, null);
//        wxapi.registerApp(APP_ID);
      //建议动态监听微信启动广播进行注册到微信
      context.registerReceiver(new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {

            // 将该 app 注册到微信
            api.registerApp(appId);
         }
      }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
   }


   public static void loginWX(){
      if(null == mContext){
         ZXToastUtil.showToast("未初始化");
         return;
      }

      if(!api.isWXAppInstalled()){
         ZXToastUtil.showToast("未安装微信客户端");
      }
      SendAuth.Req req = new SendAuth.Req();
      req.scope = "snsapi_userinfo";
      req.state = "wechat_sdk_demo_test";
      api.sendReq(req);
   }
}
